package com.bianguo.android.tencentpushdemo.view.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.adapters.ViewPagerAdapter;
import com.bianguo.android.tencentpushdemo.view.widget.TopStoryPager;

import java.util.ArrayList;

import butterknife.BindView;

public class TopStoryActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.top_store_pager)
    TopStoryPager storyPager;
    @BindView(R.id.top_pager)
    ViewPager mPager;

    private ViewPagerAdapter mPagerAdapter;
    private ArrayList<String> imageUrls;
    private int currentPosition;

    @Override
    public int layoutId() {
        return R.layout.activity_top_story;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        ArrayList<String> list = new ArrayList<>();
        list.add("http://demo.bianguo.com.cn/zhuzhenyu/scnews/Uploads/avatar/2017-05-23/5923fdd3c7358.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468555507&di=268ceae910b98448da3cdd6e119dadb4&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01c54b554113d9000001e71b1de404.jpg%401280w_1l_2o_100sh.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468610182&di=6b7980ab81fc4a44b3dacb65557d6a76&imgtype=0&src=http%3A%2F%2Fimg.ivsky.com%2Fimg%2Fbizhi%2Fpre%2F201511%2F10%2Fmercedes_benz-013.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468654167&di=c8188ba303bb6d48ecb7d166886d1195&imgtype=0&src=http%3A%2F%2Fimg.ieche.com%2F1%2F1199%2F200912%2F20091226175124337.jpg");
        storyPager.init(list);
        storyPager.startTimer(2000);

        imageUrls = new ArrayList<>();
        imageUrls.add("http://demo.bianguo.com.cn/zhuzhenyu/scnews/Uploads/avatar/2017-05-23/5923fdd3c7358.jpg");
        imageUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468555507&di=268ceae910b98448da3cdd6e119dadb4&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01c54b554113d9000001e71b1de404.jpg%401280w_1l_2o_100sh.jpg");
        imageUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468610182&di=6b7980ab81fc4a44b3dacb65557d6a76&imgtype=0&src=http%3A%2F%2Fimg.ivsky.com%2Fimg%2Fbizhi%2Fpre%2F201511%2F10%2Fmercedes_benz-013.jpg");
        imageUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468654167&di=c8188ba303bb6d48ecb7d166886d1195&imgtype=0&src=http%3A%2F%2Fimg.ieche.com%2F1%2F1199%2F200912%2F20091226175124337.jpg");
        imageUrls.add("http://demo.bianguo.com.cn/zhuzhenyu/scnews/Uploads/avatar/2017-05-23/5923fdd3c7358.jpg");
        imageUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468555507&di=268ceae910b98448da3cdd6e119dadb4&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01c54b554113d9000001e71b1de404.jpg%401280w_1l_2o_100sh.jpg");
        mPagerAdapter = new ViewPagerAdapter(mContext, imageUrls);
        mPager.setAdapter(mPagerAdapter);
        mPager.addOnPageChangeListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onDestroy() {
        storyPager.stopTimer();
        super.onDestroy();
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
//        if (imageUrls.size() > 1) { //多于1，才会循环跳转
//            if (position < 1) { //首位之前，跳转到末尾（N）
//                position = 4;
//                mPager.setCurrentItem(position, false);
//            } else if (position > 4) { //末位之后，跳转到首位（1）
//                position = 1;
//                mPager.setCurrentItem(position, false); //false:不显示跳转过程的动画
//            }
//        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //若viewpager滑动未停止，直接返回
//        if (state != ViewPager.SCROLL_STATE_IDLE) {
//            return;
//        }
        if (currentPosition == 0) {
            mPager.setCurrentItem(imageUrls.size() - 2, false);
        } else if (currentPosition == imageUrls.size() - 1) {
            mPager.setCurrentItem(1, false);
        }
    }
}
